package cn.lsnu.heritage.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import cn.lsnu.heritage.model.dto.UserloginDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lsnu.heritage.model.domain.User;
import cn.lsnu.heritage.service.UserService;
import cn.lsnu.heritage.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author LindaMan
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-01-16 21:43:58
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link UserloginDto}
     */
    @Override
    public UserloginDto login(String username, String password) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        String Password = DigestUtil.md5Hex(password);
        userQueryWrapper.eq("username",username).eq("password",Password);
        User user = userMapper.selectOne(userQueryWrapper);

        return new UserloginDto(user.getId(),user.getUsername(),user.getRole(),user.getName(),user.getAvatar());
    }

    @Override
    public boolean saveOrUpdate(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
//        true 是添加 false是更新
        if(userMapper.selectOne(userQueryWrapper)==null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 保存用户
     *
     * @param username 用户名
     * @param password 密码
     * @param name     名字
     * @param address  地址
     * @param phone    电话
     * @param age      年龄
     * @param role     角色
     * @return boolean
     */
    @Override
    public boolean saveUser(String username, String password, String name, String address, String phone, int age, int role) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(DigestUtil.md5Hex(password));
        user.setName(name);
        user.setAddress(address);
        user.setPhone(phone);
        user.setAge(age);
        user.setRole(role);
        int insert = 0;
        try {
            insert = userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(insert>0) {
            return true;
        }
        return false;
    }

    /**
     * 更新用户
     *
     * @param username 用户名
     * @param password 密码
     * @param name     名字
     * @param address  地址
     * @param phone    电话
     * @param age      年龄
     * @param role     角色
     * @return boolean
     */
    @Override
    public boolean updateUser(String username, String password, String name, String address, String phone, int age, int role) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(DigestUtil.md5Hex(password));
        user.setName(name);
        user.setAddress(address);
        user.setPhone(phone);
        user.setAge(age);
        user.setRole(role);
        QueryWrapper <User> qw = new QueryWrapper();
        qw.eq("username",username);
        int update = userMapper.update(user, qw);
        if(update>0){
            return true;
        }else {
            return false;
        }
    }
}




